﻿using System.IO.Ports;
using System.Text;

namespace SerialPortSendDemo
{
    internal class Program
    {
        private static SerialPort serialPort = new SerialPort("COM19", 9600);
        static void Main(string[] args)
        {
            serialPort.DataReceived += SerialPort_DataReceived;
            serialPort.WriteBufferSize = 100;
            serialPort.ReadBufferSize = 100;
            try
            {
                serialPort.Open();

                //发送字符串
                //serialPort.Write("发送字符串");

                Console.WriteLine("串口已连接！！！");
                Console.WriteLine("请选择：");
                Console.WriteLine("1：发送数据");
                Console.WriteLine("2：退出程序");
                string? selected = Console.ReadLine();
                if (selected == "1")
                {
                    Console.WriteLine("您选择了发送数据......");
                    string path = $"C:/Users/Administrator/Desktop/cav.html";
                    //example 1
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Open))
                        {
                            long len = fs.Length;
                            decimal val = (len / (decimal)serialPort.WriteBufferSize);
                            int p = (int)Math.Ceiling(val);
                            for (int i = 0; i < p; i++)
                            {
                                byte[] buffer = new byte[serialPort.WriteBufferSize];
                                int count = fs.Read(buffer, 0, serialPort.WriteBufferSize);
                                serialPort.Write(buffer, 0, count);
                                Thread.Sleep(50);
                            }
                        }
                    }
                    //example 2
                    //{

                    //    FileInfo fileInfo = new FileInfo(path);
                    //    long len = fileInfo.Length;
                    //    decimal val = (len / (decimal)serialPort.WriteBufferSize);
                    //    int p = (int)Math.Ceiling(val);
                    //    for (int i = 0; i < p; i++)
                    //    {
                    //        using (FileStream fs = new FileStream(path, FileMode.Open))
                    //        {
                    //            fs.Position = (i * serialPort.WriteBufferSize);
                    //            byte[] buffer = new byte[serialPort.WriteBufferSize];
                    //            int count = fs.Read(buffer, 0, serialPort.WriteBufferSize);
                    //            serialPort.Write(buffer, 0, count);
                    //            Thread.Sleep(50);
                    //        }
                    //    }
                    //}
                    Console.WriteLine("数据已发送......");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"出错：{ex.Message}");
            }
            finally
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                    serialPort.Dispose();
                }
            }
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }

        private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] buffer = new byte[serialPort.BytesToRead];
            int count = serialPort.Read(buffer, 0, buffer.Length);
            Console.WriteLine($"读取到数据：\n {Encoding.UTF8.GetString(buffer)}");
        }
    }
}
